import Taro from '@tarojs/taro'
import { LOGIN_PENDING, LOGIN_RESOLVE, LOGIN_REJECT, LOGIN_REFERSH_TOKEN } from './../constants/login';

const INITIAL_STATE = {
  token: {
    access_token: "",
    expires_in: 0,
    refresh_token: "",
    token_type: ""
  },
} 

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case LOGIN_PENDING:
      // 显示等待图标
      return {
        ...state
      }
      break
    case LOGIN_RESOLVE:
      // 显示登录成功
      // Taro.atMessage({
      //   'message': '消息通知',
      //   'type': 'info',
      // })
      Taro.setStorage({
        key:"token",
        data:action.token.access_token
      })
      return {
        ...state,
        token: action.token
      }
      break
    case LOGIN_REJECT:
      Taro.atMessage({
        'message': '登录失败',
        'type': 'error',
      })
      // 显示登录失败
      return {
        ...state
      }
      break
    case LOGIN_REFERSH_TOKEN:
      return {
        ...state,
        token: action.token
      }
    default:
      return state
      break
  }
}
